
interface TileExtendsKVPair{
    length:number
    tileExtendsMultiple:number
}

// 需要能被tileBasicLength整除
const TileExtends:TileExtendsKVPair[] = [
    {length:25000, tileExtendsMultiple:1},
    {length:50000, tileExtendsMultiple:2},
    {length:100000, tileExtendsMultiple:4},
    {length:200000, tileExtendsMultiple:6},
    {length:400000, tileExtendsMultiple:8},
    {length:800000, tileExtendsMultiple:16},
]

export class TerrainModuleC extends ModuleC<TerrainModuleS, null> {
    private lastGeneratedTileDistance = 0
    private tiles: GameObject[] = []
    
    static readonly lengthToMetre = 0.01

    private readonly tilePrefabGuid = '451CE16E4DAC620B665A11BA8465EB2E'
    private readonly tileStartPosition = new Vector(2695, 0, 0)
    private readonly tileBasicLength = 2500
    private readonly tileMaterial = [
        'EE77B1B34BCD3D3008B6F3B1BCE1C0A0',
        'A2556FAF425662B95E10808DE855AC5D',
        '1B14F6F84E6FEFFF63BCFAB3B6E9EFC4',
        '214261FB4739AB37AD5E27B44B5CBF09',
        'ECDFEF60448294CE42DB9BB07CC983F2',
        '4FB3B2B34F7E77F388753EB4F09C004C',
        'E4F0C3E54ACDF3AA4B51358BF91D3702',
    ]

    onStart(){
        this.generateTileTerrain(80000)
    }

    generateTileTerrain(distance: number){
        // 回收之前生成的块
        for (const tile of this.tiles) {
            GameObjPool.despawn(tile)
        }
        this.tiles = []

        let targetTileDistance = this.lastGeneratedTileDistance + distance
        // 从上次生成过的米数开始
        let generatedTileDistance = this.lastGeneratedTileDistance
        // 从0开始
        let forwardPosition = 0
        while(generatedTileDistance <= targetTileDistance){
            let tile = GameObjPool.spawn(this.tilePrefabGuid, GameObjPoolSourceType.Scene)
            let distanceUI = <UIWidget> tile.getChildByName('ui')
            let tileCube = <Model> tile.getChildByName('cube')
            this.tiles.push(tile)

            // 获取当前距离tile的长度
            let multiple = this.getTileExtendsMultipleByDistance(generatedTileDistance)
            let tileExtends = this.tileBasicLength * multiple
            // 赋值位置
            let position = Vector.add(this.tileStartPosition, new Vector(forwardPosition, 0, 0))
            tile.worldTransform.position = position
            // 赋值纵向缩放
            tile.worldTransform.scale = new Vector(multiple, 1, 1)
            // 赋值UI
            let txt_context = <TextBlock> distanceUI.getTargetUIWidget().rootContent.getChildByName('txt_context')
            txt_context.text = `${generatedTileDistance * TerrainModuleC.lengthToMetre} 米`
            // 赋值材质
            let material = this.tileMaterial[this.tiles.length % this.tileMaterial.length]
            tileCube.setMaterial(material)

            generatedTileDistance += tileExtends
            forwardPosition += tileExtends
        }
        this.lastGeneratedTileDistance = generatedTileDistance
        console.warn(`地图块总量:${this.tiles.length}`);
    }

    private getTileExtendsMultipleByDistance(distance:number):number{
        for (const kv of TileExtends) {
            if(distance < kv.length) return kv.tileExtendsMultiple
        }
        return TileExtends[TileExtends.length - 1].tileExtendsMultiple
    }

    private getDistanceNeedTileCount(distance:number):number{
        let leftDistance = distance
        let needCount = 0
        
        for (const kv of TileExtends) {
            let tileExtends = this.tileBasicLength * kv.tileExtendsMultiple

            if(leftDistance > kv.length){
                let count = kv.length / tileExtends
                leftDistance -= kv.length
                needCount += count
            }
            else{
                needCount += Math.ceil(leftDistance / tileExtends)
                break
            }
        }
        return needCount
    }
}

export class TerrainModuleS extends ModuleS<TerrainModuleC, null> {

}